using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class ParabolaShooter : MonoBehaviour
{
    public Transform playerTransform;           // 玩家Transform
    public GameObject warningPrefab;            // 预警红圈预制体
    public GameObject shellPrefab;              // 炮弹预制体，带有ProjectileMover脚本
    public Transform firePoint;                 // 发射点
    public float fireInterval = 3f;             // 发射间隔
    public float warningDuration = 0.5f;        // 红圈显示多久后开火
    public float forwardDistance = 5f;          // 红圈距离玩家的前方距离
    public float lateralOffsetRange = 3f;       // 左右随机偏移范围（单位）

    private List<GameObject> activeWarnings = new List<GameObject>(); // 存储所有实例化的红圈

    private Coroutine fireRoutine;

    private void Start()
    {
        fireRoutine = StartCoroutine(FireLoop());
    }

    private void OnDisable()
    {
        // 禁用所有记录下的红圈对象
        foreach (GameObject warning in activeWarnings)
        {
            if (warning != null)
            {
                warning.SetActive(false);
            }
        }
        activeWarnings.Clear();

        // 停止协程（如果脚本被禁用）
        if (fireRoutine != null)
        {
            StopCoroutine(fireRoutine);
        }
    }

    IEnumerator FireLoop()
    {
        while (true)
        {
            // 计算玩家前方 forwardDistance 米的位置
            Vector3 forwardPos = playerTransform.position + playerTransform.forward * forwardDistance;

            // 加入随机左右偏移
            float lateralOffset = Random.Range(-lateralOffsetRange, lateralOffsetRange);
            Vector3 targetPos = forwardPos + playerTransform.right * lateralOffset;

            targetPos.y = 0f; // 确保目标点在地面

            // 实例化红圈
            GameObject warning = Instantiate(warningPrefab, targetPos, Quaternion.identity);

            // 设置红圈 Y 值为 0.7f
            Vector3 warningPos = warning.transform.position;
            warningPos.y = 0.7f;
            warning.transform.position = warningPos;

            // 记录红圈
            activeWarnings.Add(warning);

            // 等待红圈持续时间
            yield return new WaitForSeconds(warningDuration);

            // 实例化炮弹
            GameObject shell = Instantiate(shellPrefab, firePoint.position, Quaternion.identity);
            shell.transform.localScale = Vector3.one * 5;
            shell.name = "enemyShell";

            // 传递目标位置给ProjectileMover脚本
            ProjectileMover mover = shell.GetComponent<ProjectileMover>();
            if (mover != null)
            {
                mover.target = targetPos;  // 传递Vector3坐标
            }
            else
            {
                Debug.LogWarning("炮弹Prefab缺少ProjectileMover脚本");
            }

            // 红圈延迟销毁（不立即移除记录，以便OnDisable能找到）
            StartCoroutine(RemoveWarningAfterDelay(warning, 1.5f));

            yield return new WaitForSeconds(fireInterval - warningDuration);
        }
    }

    IEnumerator RemoveWarningAfterDelay(GameObject warning, float delay)
    {
        yield return new WaitForSeconds(delay);
        if (warning != null)
        {
            warning.SetActive(false);
        }
        activeWarnings.Remove(warning);
    }
}
